﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using HighDivePrinter.Model;
using HighDivePrinter.Properties;
using System.Windows.Forms;


namespace HighDivePrinter 
{
    public class PrintDocumentDive : System.Drawing.Printing.PrintDocument
    {
        /// <summary>
        /// Variabel för att hålla reda på vilken sida jag skriver ut på.
        /// </summary>
        private int _printPageCounter;
        private BindingList<Diver> _list = null;
        private BindingList<Dive> _listDive = null;
        /// <summary>
        /// 
        /// </summary>C:\Users\Arsema\Documents\Visual Studio 2013\Projects\HighDive\HighDive\HighDivePrint - Copy\Model\DIVE_Title_4.jpg
        public PrintDocumentDive(BindingList<Diver> list, BindingList<Dive> divelist)
            : base()
        {

            _list = list;
            _listDive = divelist;
        }


        public void MyPrint()
        {
            _printPageCounter = 0;

            //System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
            System.Windows.Forms.PrintPreviewDialog dialog = new PrintPreviewDialog();


           this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintOnePage);

            dialog.Document = this;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Print(); // Här sker utskriften, fyrar av bl.a EVENT PrintPage
            }
        }

          /// <summary>
        /// Metod för att svara på dokumenthändelsen EVENT PrintPage för att skriva ut nästa sid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ev"></param>
        private void PrintOnePage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            Font font = new Font(FontFamily.GenericMonospace, 12);
            Font printFont = font;
            SolidBrush myBrush = new SolidBrush(Color.Black);

            int linesPerPage = (int)(ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics));
            float yPosition = 0;
            int row = 3;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            string line2 = null;
            
           // Pen p = new Pen(Color.Red, 4);
          //  Rectangle r = new Rectangle(0, 0, ev.MarginBounds.Width, ev.MarginBounds.Height);
            //ev.Graphics.DrawEllipse(p, r);
            //ev.Graphics.DrawRectangle(p, r);

            Bitmap bmp = Properties.Resources.DiverStory;
            Image newImage = bmp;
            ev.Graphics.DrawImage(newImage, 0, 0, 850, 150);

            if (_list.Count != 0)
            {
                row = 4;
                yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString("IdNumber", printFont, myBrush, (leftMargin - 80), yPosition, new StringFormat());
                ev.Graphics.DrawString("Startnum", printFont, myBrush, (leftMargin + 30), yPosition, new StringFormat());
                ev.Graphics.DrawString("First name", printFont, myBrush, (leftMargin + 150), yPosition, new StringFormat());
                ev.Graphics.DrawString("Last name", printFont, myBrush, (leftMargin + 290), yPosition, new StringFormat());
                ev.Graphics.DrawString("Gender", printFont, myBrush, (leftMargin + 420), yPosition, new StringFormat());
                ev.Graphics.DrawString("Email", printFont, myBrush, (leftMargin + 520), yPosition, new StringFormat());
                ev.Graphics.DrawString("Address", printFont, myBrush, (leftMargin + 640), yPosition, new StringFormat());

               

                row = 7;
                while (row < (linesPerPage - 7))
                {
                    line = null;
                    int i = _printPageCounter * (linesPerPage - 10) + (row - 7);
                    if (i >= _list.Count)
                        break;

                    Diver diver = _list[i];
                    line = diver.IdNumber +"      " + diver.StartNumber + "         " + diver.FirstName + "       " + diver.LastName + "        " + diver.Gender + "   " + diver.Email + "  " + diver.Address;// +" " + dive.TotalScore;

             
                    yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                    ev.Graphics.DrawString(line, printFont, myBrush, (leftMargin-70), yPosition, new StringFormat());

                    row++;

                  
                }
            }

           

            if (_listDive.Count != 0)
            {
                
                
                row = 17;
                Bitmap bmp_list = Properties.Resources.DiveLIST;
                Image ListImage = bmp_list;
                ev.Graphics.DrawImage(ListImage, 0, 350, 350, 60);
                
                row = 17;
                yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString("Code", printFont, myBrush, (leftMargin), yPosition, new StringFormat());
                ev.Graphics.DrawString("Dive Number", printFont, myBrush, (leftMargin + 90), yPosition, new StringFormat());
                ev.Graphics.DrawString("Height", printFont, myBrush, (leftMargin + 230), yPosition, new StringFormat());
                ev.Graphics.DrawString("Difficulty", printFont, myBrush, (leftMargin + 320), yPosition, new StringFormat());
                ev.Graphics.DrawString("TotalScore", printFont, myBrush, (leftMargin + 470), yPosition, new StringFormat());

                row = 19;
                while (row < (linesPerPage - 10))
                {
                    
                    int i = _printPageCounter * (linesPerPage - 49) + (row - 19);
                    if ((i >= _listDive.Count))
                        break;

                   
                    Dive dive = _listDive[i];
                    line2 = dive.DiveCode + "         " + dive.DiveNumber + "           " + dive.DiveHeight + "          " + dive.Difficulty + "          " + dive.TotalScore;

                    yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
                    ev.Graphics.DrawString(line2, printFont, myBrush, (leftMargin), yPosition, new StringFormat());

                    row++;
                }
            }
            row++;
            yPosition = topMargin + row * printFont.GetHeight(ev.Graphics);
            ev.Graphics.DrawString(_printPageCounter.ToString(), printFont, myBrush, leftMargin, yPosition, new StringFormat());

            // If there are more lines, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
            myBrush.Dispose();

            this._printPageCounter++;
        } 

    
       /* public PrintDocumentDive(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }*/
    }
}
